﻿using System.Collections.Generic;
using System.Linq;
using Zadani09.db.DAO;
using System.Reflection;

namespace Zadani09.db.DAOimpl
{
    public class DiloDAOmysql : AbstractDAO, IDiloDAO
    {

        public void create(Dilo dilo)
        {
            initiateConnection();

            db.Dilo.InsertOnSubmit(dilo);
            db.SubmitChanges();

            closeConnection();
        }

        public Dilo load(string id)
        {
            initiateConnection();

            uint idu = uint.Parse(id);

            var result = from d in db.Dilo
                         where d.IDDilo == idu
                         select new Dilo()
                         {
                             IDDilo = d.IDDilo,
                             Nazev = d.Nazev,
                             Popis = d.Popis,
                             DlouhyPopis = d.DlouhyPopis,
                             Datum = d.Datum,
                             NaProdej = d.NaProdej,
                             Ulice = d.Ulice,
                             Popisne = d.Popisne,
                             Mesto = d.Mesto,
                             PSC = d.PSC

                         };


            closeConnection();

            Dilo nahraneDilo = null;

            List<Dilo> list = new List<Dilo>();

            foreach (var item in result)
            {
                list.Add(item);
            }


            if (list.Count > 1 || list.Count < 1)
            {
                //throw exeption nejakou...
                nahraneDilo = null;
            }
            else
            {
                nahraneDilo = list.First<Dilo>();
            }

            return nahraneDilo;
        }

        public void delete(Dilo dilo)
        {
            initiateConnection();

            db.Dilo.Attach(dilo);
            db.Dilo.DeleteOnSubmit(dilo);

            db.SubmitChanges();

            closeConnection();
        }

        public void delete(string id)
        {
            Dilo dilo = new Dilo();
            dilo.IDDilo = uint.Parse(id);

            this.delete(dilo);
        }

        public System.Collections.Generic.List<Dilo> loadList(int odkud, int kolik)
        {
            initiateConnection();

            var result = from d in db.Dilo select d;

            List<Dilo> list = result.ToList<Dilo>();

            closeConnection();

            return list;
        }


        public void update(Dilo nove)
        {
            initiateConnection();

            uint id = nove.IDDilo;

            var updateQ = db.Dilo.First(e => e.IDDilo == id);

            //id upravovanych entit se nemeni..
            updateQ.Nazev = nove.Nazev;
            updateQ.Popis = nove.Popis;
            updateQ.DlouhyPopis = nove.DlouhyPopis;
            updateQ.Datum = nove.Datum;
            updateQ.NaProdej = nove.NaProdej;
            updateQ.Mesto = nove.Mesto;
            updateQ.Popisne = nove.Popisne;
            updateQ.PSC = nove.PSC;
            updateQ.Ulice = nove.Ulice;

            db.SubmitChanges();


            closeConnection();
        }

        public List<Dilo> nahrejDilaZvystavy(int idVystavy)
        {
            initiateConnection();

            IQueryable<uint> result1 = from o in db.VystavaDilo
                                       where o.IDVystava == (uint)idVystavy
                                       select o.IDDilo;

            IQueryable<Dilo> result2 = from d in db.Dilo select d;

            closeConnection();

              // zadne dila
            if (result1 == null) return (null);

            List<uint> pouzite;
            List<Dilo> vrat = new List<Dilo>();

            try
            {
                pouzite = result1.ToList<uint>();
            }
            catch (TargetParameterCountException)
            {
                return (vrat);
            }
            
            foreach (Dilo jeden in result2)
            {
                if (pouzite.Contains(jeden.IDDilo))
                {
                    vrat.Add(jeden);
                }
            }

            if (vrat.Count <= 0) vrat = null;

            return (vrat);
        }

        public List<Dilo> nahrejDilaVserii(int idSerie)
        {
            initiateConnection();

            IQueryable<uint> result1 = from s in db.SerieDilo
                                       where s.IDSerie == (uint)idSerie
                                       select s.IDDilo;

            IQueryable<Dilo> result2 = from d in db.Dilo select d;

            closeConnection();

            List<Dilo> vrat = new List<Dilo>();
            List<uint> pouzite = null;

              // zadne dila
            if (result1 == null) return (null);

            try
            {
                pouzite = result1.ToList<uint>();
            }
            catch (TargetParameterCountException)
            {
                return (vrat);
            }

            foreach (Dilo jeden in result2)
            {
                if (pouzite.Contains(jeden.IDDilo))
                {
                    vrat.Add(jeden);
                }
            }

            if (vrat.Count <= 0) vrat = null;

            return (vrat);
        }
    }
}
